package yja.gzw.app.adapter

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.kongzue.dialogx.dialogs.CustomDialog
import com.kongzue.dialogx.interfaces.OnBindView
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuichat.classicui.page.TUIC2CChatActivity
import yja.gzw.app.R
import yja.gzw.app.activity.BianjizhuanquActivity
import yja.gzw.app.activity.FabidingdanActivity
import yja.gzw.app.activity.WodedindanXiangqingActicity
import yja.gzw.app.gedingdanzhuangtai.ChexiaoActivity
import yja.gzw.app.gedingdanzhuangtai.DaifahuoActivity
import yja.gzw.app.gedingdanzhuangtai.DaipingjiaActivity
import yja.gzw.app.gedingdanzhuangtai.DaiquerengActivity
import yja.gzw.app.gedingdanzhuangtai.DaishouhuoActivity
import yja.gzw.app.gedingdanzhuangtai.FabuzhongActivity
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.OrderDatafa
import yja.gzw.app.model.bean.OrderDatafa2
import yja.gzw.app.viewmodel.Mainviewmodel
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class Fabudingdan2Adapter2  (private val content: Context, private val list: MutableList<OrderDatafa2>, private val lifecycleOwner: androidx.lifecycle.LifecycleOwner): RecyclerView.Adapter<RecyclerView.ViewHolder>(){
    private var view: View?=null
    val viewmodel= Mainviewmodel()
    private lateinit var bottomSheetDialog: BottomSheetDialog
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        view= LayoutInflater.from(parent.context).inflate(R.layout.item_view_dindanshow2,parent,false)
        return ViewHodeView(view!!)
    }

    override fun getItemCount(): Int {
       return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if(holder is ViewHodeView){

            when(list[position].status){
                0->{
                    holder.zhuangtai.text="未支付"
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
                    holder.bianji.visibility=View.GONE
                    holder.shanchu.visibility=View.GONE
                    holder.sixing.visibility=View.GONE
                    holder.queding.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE

                }
                1->{
                    holder.zhuangtai.text="发布中"
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.huangh))

                    holder.bianji.visibility=View.VISIBLE
                    holder.shanchu.visibility=View.GONE


                    holder.sixing.visibility=View.GONE
                    holder.queding.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE
                }
                2->{
                    holder.zhuangtai.text="待确认"
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.huangh))
                    holder.sixing.visibility=View.VISIBLE
                    holder.queding.visibility=View.VISIBLE

                    holder.bianji.visibility=View.GONE
                    holder.shanchu.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE


                }
                3->{
                    holder.sixing.visibility=View.VISIBLE
                    holder.zhuangtai.text="待发货"
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hei))

                    holder.bianji.visibility=View.GONE
                    holder.shanchu.visibility=View.GONE

                    holder.queding.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE

                }
                4->{
                    holder.sixing.visibility=View.VISIBLE
                    holder.zhuangtai.text="待收货"
                    holder.quedingshouhuo.visibility=View.VISIBLE
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))

                    holder.bianji.visibility=View.GONE
                    holder.shanchu.visibility=View.GONE
                    holder.queding.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE

                }
                5->{
                    holder.sixing.visibility=View.GONE
                    holder.zhuangtai.text="待评价"
                    holder.shanchu.visibility=View.VISIBLE
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hei))
                    holder.pingjia.visibility=View.VISIBLE

                    holder.bianji.visibility=View.GONE


                    holder.queding.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE

                }
                -1->{
                    holder.shanchu.visibility=View.VISIBLE
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
                    holder.zhuangtai.text="撤销订单"

                    holder.bianji.visibility=View.GONE
                    holder.sixing.visibility=View.GONE
                    holder.queding.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE
                }
                -2->{
                    holder.shanchu.visibility=View.VISIBLE
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
                    holder.zhuangtai.text="撤销订单待后台确认"

                    holder.bianji.visibility=View.GONE
                    holder.sixing.visibility=View.GONE
                    holder.queding.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE
                }

                -3->{
                    holder.shanchu.visibility=View.VISIBLE
                    holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
                    holder.zhuangtai.text="撤销订单已完成"

                    holder.bianji.visibility=View.GONE

                    holder.sixing.visibility=View.GONE
                    holder.queding.visibility=View.GONE
                    holder.quedingshouhuo.visibility=View.GONE
                    holder.pingjia.visibility=View.GONE
                }


            }

            viewmodel.onxiangqingwang(UserData.token, mapOf("order_id" to list[position].id.toString()))

            viewmodel.onxiangqing.observe(lifecycleOwner, Observer {
                if(it.code==1){


                    val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
                    Glide.with(content).load("http://117ai.32.hzgqapp.com/"+it.data.price_list[0].avatar)
                        .apply(requestOptions)
                        .into(holder.usertouxiang)
                    holder.name.text=it.data.price_list[0].username


                }else{
                    Toast.makeText(content,it.msg, Toast.LENGTH_SHORT).show()
                }

            })




            val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
            holder.name.text=list[position].username
//            Glide.with(content).load("http://117ai.32.hzgqapp.com/"+list[position].userinfo.avatar)
//                .apply(requestOptions)
//                .into(holder.usertouxiang)





            holder.sixing.setOnClickListener {
                val intent = Intent(content, TUIC2CChatActivity::class.java)
//                intent.putExtra(TUIConstants.TUIChat.CHAT_ID,list[position].userinfo.id.toString());
                intent.putExtra(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
              content.  startActivity(intent)
            }


            holder.dindanhao.text=list[position].order_no
            if(list[position].type=="1"){

                holder. zhuanuqleixing.text="三元专区"

            }else if(list[position].type=="2"){

                holder. zhuanuqleixing.text="四元专区"
            }else{
                holder. zhuanuqleixing.text="精品视频"
                //holder.shanchu.visibility=View.GONE

            }
            holder. zhuanquname.text=list[position].title
            if(list[position].release_type==1){
                holder.fabumoshi.text="自己使用ai生成视频"

            }else if(list[position].release_type==2){
                holder.fabumoshi.text="第二方使用ai生成视频"
            }else{
                holder.fabumoshi.text="不使用ai生成视频"
            }

            holder.bianji.setOnClickListener {

                Log.e("点击了","111")


                val inttnt= Intent(content,BianjizhuanquActivity::class.java)
                inttnt.putExtra("id",list[position].id.toString())
                content.startActivity(inttnt)
            }


            holder.itemView.setOnClickListener {
                //todo跳转不同的页面


                if( list[position].status==1){
                    val intent=Intent(content,FabuzhongActivity::class.java)
                    intent.putExtra("ids",list[position].id.toString())
                    intent.putExtra("type",list[position].type)
                    content.startActivity(intent)


                }else if(list[position].status==2){
                    val intent=Intent(content,DaiquerengActivity::class.java)
                    intent.putExtra("ids",list[position].id.toString())
                    intent.putExtra("type",list[position].type)
                    content.startActivity(intent)



                }  else if(list[position].status==3){
                    val intent=Intent(content,DaifahuoActivity::class.java)
                    intent.putExtra("type",list[position].type)
                    intent.putExtra("ids",list[position].id.toString())
                    content.startActivity(intent)


                } else if(list[position].status==4){
                    val intent=Intent(content,DaishouhuoActivity::class.java)
                    intent.putExtra("type",list[position].type)
                    intent.putExtra("ids",list[position].id.toString())
                    content.startActivity(intent)
                }else if(list[position].status==5){
                    val intent=Intent(content,DaipingjiaActivity::class.java)
                    intent.putExtra("type",list[position].type)
                    intent.putExtra("ids",list[position].id.toString())
                    content.startActivity(intent)

                }
                else{
                    val intent=Intent(content,ChexiaoActivity::class.java)
                    intent.putExtra("type",list[position].type)
                    intent.putExtra("ids",list[position].id.toString())
                    content.startActivity(intent)

                }


            }



            holder.shanchu.setOnClickListener {


                CustomDialog.show(object : OnBindView<CustomDialog>(R.layout.layout_custom_tishi) {
                    override fun onBind(dialog: CustomDialog, v: View) {
                        val tongyi = v.findViewById<TextView>(R.id.tongyi)
                        val butongyi = v.findViewById<TextView>(R.id.butongyi)

                        tongyi.setOnClickListener {
                            viewmodel.onshanchudingdanwang(UserData.token,mapOf("id" to list[position].id.toString()))
                            viewmodel.onshanchudingdan.observe(content as FabidingdanActivity, Observer {
                                if(it.code==1){
                                    list.removeAt(position)
                                    dialog.dismiss()
                                    // 通知适配器数据集已更改
                                    notifyItemRemoved(position)
                                    notifyItemRangeChanged(position, list.size - position)

                                }else{
                                    Toast.makeText(content,it.msg, Toast.LENGTH_SHORT).show()

                                }

                            })

                        }

                        butongyi.setOnClickListener {
                            dialog.dismiss()
                        }



                    }
                })
                    .setMaskColor(content.getColor(R.color.touming))


            }

        }

    }
    inner class ViewHodeView(itemView: View):RecyclerView.ViewHolder(itemView){

        val dindanhao: TextView =itemView.findViewById(R.id.dindanbiaohao)
        val zhuanuqleixing: TextView =itemView.findViewById(R.id.cishu)
        val zhuanquname: TextView =itemView.findViewById(R.id.zhuanquname)
        val fabumoshi: TextView =itemView.findViewById(R.id.fabupingtai)
        val shanchu: TextView =itemView.findViewById(R.id.shanchu)
        val bianji: TextView =itemView.findViewById(R.id.bianji)
        val zhuangtai: TextView =itemView.findViewById(R.id.zhuangtai)
        val usertouxiang: ImageView =itemView.findViewById(R.id.usertouxiang)
        val name: TextView =itemView.findViewById(R.id.name)

        val sixing: TextView =itemView.findViewById(R.id.sixing)
        val queding: TextView =itemView.findViewById(R.id.queding)
        val quedingshouhuo: TextView =itemView.findViewById(R.id.quedingshouhuo)
        val pingjia: TextView =itemView.findViewById(R.id.pingjia)
    }

}